<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use DB;

class configureController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $configure = DB::table('configure')->select()->get();
        // dd($configure);
        return view('admin.configure.details', ['configure'=>$configure]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $configure = DB::table('configure')->where('id', $id)->select()->get()->first();
        return view('admin.configure.edit', ['id'=>$id,'configure'=>$configure]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // 判断是否有文件上传
        if($request->hasFile('logo')){
            // 判断上传的文件是否有效
            if($request->file('logo')->isValid()){
                // 获取上传的文件对象
                $data = $request->file('logo');                
                // 获取上传的文件的后缀
                $ext = $data->getClientOriginalExtension();
                // 拼装出你需要使用的文件名
                $picname = time().rand(1000,9999).'.'.$ext;
                // 移动临时文件，生成新文件到指定目录下
                $data->move('./admin/upload',$picname);                
                $configure = $request->except('_token','_method');
                $configure['logo'] = $picname;

                // dd($configure);
                $row = DB::table('configure')->where('id', $id)->update($configure);
                if($row > 0){
                    return redirect('admin/configure/details')->with('msg','修改成功');
                }else{
                    return redirect('admin/configure/details')->with('error','修改失败');
                }
            }
        }
        $configure = $request->except('_token','_method');
        // dd($configure);
        $row = DB::table('configure')->where('id', $id)->update($configure);
        if($row > 0){
            return redirect('admin/configure/details')->with('msg','修改成功');
        }else{
            return redirect('admin/configure/details')->with('error','修改失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}